package com.mercury.service;

import com.alibaba.fastjson.JSONObject;
import com.mercury.dao.IPcuserinfoDao;
import com.mercury.entity.Pcuserinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class PcuserinfoService {

    @Autowired
    IPcuserinfoDao pcuserinfoDao;

    public Pcuserinfo getUserinfo(JSONObject jsonObject) {
        if (jsonObject.getString("name") != null) {
            Pcuserinfo pcuserinfo = new Pcuserinfo();
            pcuserinfo.setGuid(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + "0001");
            pcuserinfo.setName(jsonObject.getString("name"));
            pcuserinfo.setSchool(jsonObject.getString("school"));
            pcuserinfo.setOpenid(jsonObject.getString("openid"));
            pcuserinfo.setCode(UUID.randomUUID().toString());
            pcuserinfoDao.create(pcuserinfo);
            return pcuserinfo;
        } else {
            return pcuserinfoDao.queryByOpenid(jsonObject.getString("openid"));
        }
    }
}
